﻿#region Directives

using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Criterion;
using My.Data.BusinessObject;
using System.Reflection;
using NHibernate.Mapping;
using System.Collections;

#endregion

namespace My.Data.DataAccess
{
    public partial interface ILoginDao
    {
        // Get Methods
        Login GetById(int Id);
        IList<Login> GetAll();

        // Misc Methods
        void SetFetchMode(string associationPath, FetchMode mode);
        //ICriteria CreateCriteria();

        // CRUD Methods
        object Save(Login entity);
        void SaveOrUpdate(Login entity);
        void Delete(Login entity);
        void Update(Login entity);

        // Properties
        System.Type Type { get; }
        ISession ISession { get; }
    }

    class LoginDao : ILoginDao
    {
        #region Declarations

        protected ISession _ISession;
        protected const int defaultMaxResults = 100;
        private Dictionary<string, FetchMode> _fetchModeMap = new Dictionary<string, FetchMode>();

        #endregion

        #region Properties

        /// <summary>
        /// Gets the type.
        /// </summary>
        /// <value>The type.</value>
        public System.Type Type
        {
            get { return typeof(Login); }
        }

        /// <summary>
        /// Gets the session.
        /// </summary>
        /// <value>The session.</value>
        public ISession ISession
        {
            get { return _ISession; }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="PersonDao"/> class.
        /// </summary>
        /// <param name="session">The session.</param>
        public LoginDao(ISession session)
        {
            this._ISession = session;
        }

        #endregion

        #region Misc Methods

        /// <summary>
        /// Sets the fetch mode.
        /// </summary>
        /// <param name="associationPath">The association path.</param>
        /// <param name="mode">The mode.</param>
        public void SetFetchMode(string associationPath, FetchMode mode)
        {
            if (!_fetchModeMap.ContainsKey(associationPath))
                _fetchModeMap.Add(associationPath, mode);
        }

        /// <summary>
        /// Creates the criteria.
        /// </summary>
        /// <returns></returns>
        public ICriteria CreateCriteria()
        {
            ICriteria criteria = ISession.CreateCriteria(typeof(Login));

            foreach (var pair in _fetchModeMap)
                criteria = criteria.SetFetchMode(pair.Key, pair.Value);

            return criteria;
        }

        #endregion

        #region CRUD Methods

        /// <summary>
        /// Gets the by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public virtual Login GetById(int id)
        {
            return (Login)ISession.Get(typeof(Login), id);
        }

        public IList<Login> GetAll()
        {
            ICriteria targetObjects = ISession.CreateCriteria(typeof(Login));
            IList<Login> itemList = targetObjects.List<Login>();
            return itemList;
        }

        /// <summary>
        /// Saves the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public object Save(Login entity)
        {
            return ISession.Save(entity);
        }

        /// <summary>
        /// Saves the or update.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public void SaveOrUpdate(Login entity)
        {
            ISession.SaveOrUpdate(entity);
        }

        /// <summary>
        /// Deletes the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public void Delete(Login entity)
        {
            ISession.Delete(entity);
        }

        /// <summary>
        /// Updates the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public void Update(Login entity)
        {
            ISession.Update(entity);
        }

        #endregion
    }
}
